import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      component: () => import('@/pages/home/index'),
      meta: { title: '校园首页', icon: 'dashboard' },
      hidden: true
    }]
  },

  // 角色权限
  {
    path: '/role',
    component: Layout,
    redirect: '/role',
    name: 'Role',
    meta: { title: '角色权限', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/pages/role/user/index'),
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'user_details',
        name: 'User_details',
        component: () => import('@/pages/role/user/details'),
        // meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'permission',
        name: 'Permission',
        component: () => import('@/pages/role/permission/index'),
        meta: { title: '权限管理', icon: 'tree' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/pages/role/role/index'),
        meta: { title: '角色管理', icon: 'tree' }
      }
    ]
  },

  // 物品管理
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods',
    name: 'Goods',
    meta: { title: '物品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import('@/pages/goods/inventory/index'),
        meta: { title: '库存管理', icon: 'table' }
      },
      {
        path: 'receive',
        name: 'Receive',
        component: () => import('@/pages/goods/receive/index'),
        meta: { title: '我领用的', icon: 'tree' }
      },
      {
        path: 'borrow',
        name: 'Borrow',
        component: () => import('@/pages/goods/borrow/index'),
        meta: { title: '我借用的', icon: 'tree' }
      },
      {
        path: 'purchase',
        name: 'Purchase',
        component: () => import('@/pages/goods/purchase/index'),
        meta: { title: '我申购的', icon: 'table' }
      },
      {
        path: 'procure',
        name: 'Procure',
        component: () => import('@/pages/goods/procure/index'),
        meta: { title: '采购清单', icon: 'tree' }
      },
      {
        path: 'outbound',
        name: 'Outbound',
        component: () => import('@/pages/goods/outbound/index'),
        meta: { title: '出库清单', icon: 'tree' }
      },
      {
        path: 'apply',
        name: 'Apply',
        component: () => import('@/pages/goods/apply/index'),
        meta: { title: '物品申领', icon: 'table' }
      },
      {
        path: 'type',
        name: 'Type',
        component: () => import('@/pages/goods/type/index'),
        meta: { title: '类型管理', icon: 'tree' }
      }
    ]
  },

  // 审核管理
  {
    path: '/audit',
    component: Layout,
    redirect: '/audit',
    children: [{
      path: 'audit',
      name: 'audit',
      component: () => import('@/pages/audit/index'),
      meta: { title: '审核管理', icon: 'dashboard' }
    }]
  },

  // 物品统计
  {
    path: '/statistical',
    component: Layout,
    redirect: '/statistical',
    name: 'Statistical',
    meta: { title: '物品统计', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'receive',
        name: 'Receive',
        component: () => import('@/pages/statistical/receive/index'),
        meta: { title: '领用统计', icon: 'table' }
      },
      {
        path: 'purchase',
        name: 'Purchase',
        component: () => import('@/pages/statistical/purchase/index'),
        meta: { title: '申购统计', icon: 'tree' }
      },
      {
        path: 'borrow',
        name: 'Borrow',
        component: () => import('@/pages/statistical/borrow/index'),
        meta: { title: '借用统计', icon: 'tree' }
      }
    ]
  },

  // 日志列表
  {
    path: '/log',
    component: Layout,
    redirect: '/log',
    children: [{
      path: 'log',
      name: 'Log',
      component: () => import('@/pages/log/index'),
      meta: { title: '日志管理', icon: 'dashboard' }
    }]
  },



  

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
